package me.zhengjie.minbearwcs.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.dao.config.inf.WcsParamConfigDao;
import me.zhengjie.minbearwcs.dao.stock.inf.MaterialTypeDao;
import me.zhengjie.minbearwcs.entity.PO.config.WcsParamConfig;
import me.zhengjie.minbearwcs.entity.PO.config.WcsParamConfigContentPool;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.MaterialType;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.type.StorageType;
import me.zhengjie.minbearwcs.utils.WcsHpptUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WcsInitConfig {

    private final WcsHpptUtil wcsHpptUtil;
    private final MaterialTypeDao materialTypeDao;
    private final WcsParamConfigDao wcsParamConfigDao;

    /**
     * 开机获取物料信息
     */
    @PostConstruct
    @Transactional
    public void initGetMaterialType() {
        try {
            Long count = materialTypeDao.getCount();
            if (count < 50) {

                List<MaterialType> materialTypes = wcsHpptUtil.fetchMaterialTypeList("2000-01-01");
                log.info("开机，获取wms 物料信息 ： {}", materialTypes);

                for (MaterialType materialType : materialTypes) {
                    MaterialType materialTypeTemp = materialTypeDao.selectByMaterialCode(materialType.getMaterialCode());
                    if (materialTypeTemp == null) {
                        materialTypeDao.insert(materialType);
                    } else {
                        materialType.setId(materialTypeTemp.getId());
                        materialTypeDao.update(materialType);
                    }

                }
            }
        } catch (Exception e) {
            log.error("开机，获取wms 物料信息失败！！", e);
        }
    }


    /**
     * 1F出库储位选择初始化
     */
    @PostConstruct
    @Transactional
    public void initOutboundDestinationType() {
        try {
            WcsParamConfig wcsParamConfig = wcsParamConfigDao.selectByKey(WcsParamConfigContentPool.OUTBOUND_DESTINATION_TYPE);
            if (wcsParamConfig == null) {
                wcsParamConfigDao.insert(new WcsParamConfig(null, WcsParamConfigContentPool.OUTBOUND_DESTINATION_TYPE, StorageType.WHARF_1F.name()));
            }
        } catch (Exception e) {
            log.error("1F出库储位选择初始化失败！！", e);
        }
    }
}
